﻿using System;
using Microsoft.ApplicationBlocks.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace DataAccess
{
    public class NhaCungCapDAO
    {
        private static readonly String URL = ConfigurationSettings.AppSettings["Main.ConnectionString"];
        public NhaCungCapDAO()
        {

        }

        public static DataSet GetAll()
        {
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM NhaCungCap");
        }

        public static void Delete(int id) 
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            sqlParameter[0] = new SqlParameter("@MaNhaCungCap", id);
            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "DELETE FROM NhaCungCap WHERE MaNhaCungCap = @MaNhaCungCap", sqlParameter);
        }

        public static void Insert(string name,string diachi, string dienthoai, string email, string website)
        {
            SqlParameter[] sqlParameter = new SqlParameter[5];
            sqlParameter[0] = new SqlParameter("@TenNhaCungCap",name);
            sqlParameter[1] = new SqlParameter("@DiaChi", diachi);
            sqlParameter[2] = new SqlParameter("@DienThoai",dienthoai);
            sqlParameter[3] = new SqlParameter("@Email", email);
            sqlParameter[4] = new SqlParameter("@Website", website);
            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "INSERT INTO NhaCungCap VALUES(@TenNhaCungCap,@DiaChi,@DienThoai,@Email,@Website)", sqlParameter);
        }

        public static void Update(int id,string name,string diachi, string dienthoai, string email,string website)
        {
            SqlParameter[] sqlParameter = new SqlParameter[6];
            sqlParameter[0] = new SqlParameter("@TenNhaCungCap", name);
            sqlParameter[1] = new SqlParameter("@DiaChi", diachi);
            sqlParameter[2] = new SqlParameter("@DienThoai", dienthoai);
            sqlParameter[3] = new SqlParameter("@Email", email);
            sqlParameter[4] = new SqlParameter("@Website", website);
            sqlParameter[5] = new SqlParameter("@MaNhaCungCap", id);

            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "UPDATE NhaCungCap SET TenNhaCungCap = @TenNhaCungCap,DiaChi = @DiaChi,DienThoai = @DienThoai,Email = @Email,Website = @Website WHERE MaNhaCungCap = @MaNhaCungCap", sqlParameter);
        }
        public static DataSet GetBy(int id)
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            sqlParameter[0] = new SqlParameter("@MaNhaCungCap", id);
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM NhaCungCap WHERE MaNhaCungCap = @MaNhaCungCap", sqlParameter);
        }
    }
}
